#include<stdio.h>
#include<string.h>

int main()
{
	char goal[100]={'\0'};
	int key=0,sign,flag,n;
	char c[100][100]={'\0'};
	scanf("%s %d %d",&goal,&key,&n);
	for(sign=0;sign<n;sign++)
	{
		scanf("%s",&c[sign]);
		if(key==0) strlwr(c[sign]);
	}
	if(key==0) strlwr(goal);
	for(sign=0;sign<n;sign++)
	{
		if(strstr(c[sign],goal)!=NULL) puts(c[sign]);
	}
	return 0;
}
